package com.jdk.demo.fileChannel.batchRead;

import java.io.File;
import java.io.FileInputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class Test4_2 {

	private static FileInputStream fisRef;
	private static FileChannel fileChannel;
	
	// 验证read方法从通道的当前位置开始读取；将字节放入ByteBuffer当前位置
	public static void main(String[] args) throws Exception {
		fisRef = new FileInputStream(new File("d.txt"));
		fileChannel = fisRef.getChannel();
		fileChannel.position(2);
		
		ByteBuffer buffer1 = ByteBuffer.allocate(2);
		ByteBuffer buffer2 = ByteBuffer.allocate(2);
		ByteBuffer[] bufferArray = new ByteBuffer[]{buffer1, buffer2};
		
		buffer1.position(1);
		fileChannel.read(bufferArray);

		for (int j = 0; j < bufferArray.length; j++) {
			byte[] getByte = bufferArray[j].array();
			for (int k = 0; k < getByte.length; k++) {
				if(getByte[k] == 0){
					System.out.print(" 空格 ");
				}else{
					System.out.print((char)getByte[k]);
				}
			}
			System.out.println();
		}
		
		// 输出   " 空格 c"
		// 		"de"
		fileChannel.close();
		fisRef.close();
	}
	
}
